﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MayhemCore;
using System.Runtime.Serialization;
using MayhemWpf.ModuleTypes;
using MayhemWpf.UserControls;
using System.ComponentModel;
using System.Threading;
using ComputerMayhem.Wpf;
using Microsoft.WindowsAPICodePack.ApplicationServices;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Forms;

namespace ComputerMayhem.Events
{

    [DataContract]
    [MayhemModule("Power Source", "Monitors your power source")]
    public class PowerSource : EventBase, IWpfConfigurable
    {
        [DataMember]
        private Microsoft.WindowsAPICodePack.ApplicationServices.PowerSource powerSource;

        protected override void OnLoadDefaults()
        {
            powerSource = Microsoft.WindowsAPICodePack.ApplicationServices.PowerSource.AC;
        }

        protected override void OnAfterLoad()
        {
            PowerManager.PowerSourceChanged +=new EventHandler(PowerManager_PowerSourceChanged);
        }

        void PowerManager_PowerSourceChanged(object sender, EventArgs e)
        {
            if (PowerManager.PowerSource == powerSource)
            { 
                Trigger();
            }
        }

        protected override void OnDeleted()
        {
            PowerManager.PowerSourceChanged -= new EventHandler(PowerManager_PowerSourceChanged);
        }

        public WpfConfiguration ConfigurationControl
        {
            get
            {
                return new PowerSourceConfig(powerSource);
            }
        }

        public void OnSaved(WpfConfiguration configurationControl)
        {
            PowerSourceConfig config = configurationControl as PowerSourceConfig;
            powerSource = config.PowerSource;
        }

        public string GetConfigString()
        {
            return "Power source changed to " + powerSource.ToString();
        }
    }
}
